package com.dev;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.fb.FileBrowserPage;
import com.log;
import java.io.File;
import java.io.FileReader;
public class QueryOpenFile extends LinearLayout implements OnClickListener{
	private WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	public boolean openInNewTab;
	public TextView title;
	LinearLayout titlebar;
	LinearLayout bottombar;
	IconView close;
	Item itText,itSelected,itHtml;
	TextButton tbOpen;
	LayoutParams tblp;
	Bitmap tick;
	public File destination;
	FileBrowserPage fbp;
	MainActivity ma;
	public QueryOpenFile(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setWillNotDraw(false);
		title=new TextView(ma);
		title.setTextColor(0xff000000);
		title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.setPadding(9,0,0,0);
		close=new IconViewClose(ma);
		close.setOnClickListener(this);
		titlebar=new LinearLayout(ma){
			public void onDraw(Canvas cvs){
				super.onDraw(cvs);
				Paint p=new Paint();
				p.setColor(FileBrowserPage.borderColor);
				cvs.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
			}
		};
		titlebar.setWillNotDraw(false);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.addView(title,new LayoutParams(0,-1,1));
		titlebar.addView(close);
		tblp=new LayoutParams(0,-1,1);
		tbOpen=new TextButton(ma,"打开");
		tbOpen.borderColor=FileBrowserPage.borderColor;
		tbOpen.borderWidth=FileBrowserPage.borderWidth;
		tbOpen.setOnClickListener(this);
		bottombar=new LinearLayout(ma){
			@Override
			public void onDraw(Canvas c){
				super.onDraw(c);
				Paint p=new Paint();
				p.setColor(FileBrowserPage.borderColor);
				c.drawRect(0,0,getWidth(),FileBrowserPage.borderWidth,p);
			}
		};
		bottombar.setWillNotDraw(false);
		bottombar.setOrientation(HORIZONTAL);
		bottombar.addView(tbOpen,tblp);
		itText=new Item("text");
		itText.setOnClickListener(this);
		itHtml=new Item("html");
		itHtml.setOnClickListener(this);
		itSelected=itText;
		addView(titlebar,-1,ma.dialogTitleBarHeight);
		addView(itText,-1,-2);
		addView(itHtml,-1,-2);
		addView(bottombar,-1,(int)(30*ma.dp));
		tick=BitmapFactory.decodeResource(ma.getResources(),R.drawable.ic_tick);
	}
	@Override
	public void onClick(View v){
		if(v==close){
			ma.wm.removeView(this);
		}else if(v==tbOpen){
			if(openInNewTab){
				if(itSelected==itHtml){
					HtmlEditorTab nt=new HtmlEditorTab(ma);
					nt.ni=new TabData.HtmlBrowserInfo();
					nt.ni.src=destination;
					nt.setOnClickListener(ma.gui.TabOnClick);
					nt.setText(destination.getName());
					ma.tabList.addTabInfo(nt.ni);
					ma.tdm.updateSavedTabList(ma.tabList);
					ma.gui.tabs.addView(nt,TabBar.tablp);
					ma.gui.TabOnClick.onClick(nt);
				}
			}else if(itSelected==itHtml){
				fbp.dlv.setVisibility(View.GONE);
				fbp.fn.setVisibility(View.GONE);
				fbp.toolsbar.setVisibility(View.GONE);
				ma.gui.ivQuit.setVisibility(View.VISIBLE);
				ma.gui.ivInto.setVisibility(View.GONE);
				ma.gui.ivInfo.setVisibility(View.VISIBLE);
				ma.gui.ivRW.setImageResource(R.drawable.ic_lead_pencil);
				ma.gui.ivRW.setVisibility(View.VISIBLE);
				ma.gui.ivSave.setColorFilter(IconView.unable);
				ma.gui.ivSave.setVisibility(View.VISIBLE);
				ma.gui.ivRefresh.setColorFilter(IconView.unable);
				ma.gui.ivRefresh.setVisibility(View.VISIBLE);
				ma.gui.status.setText(destination.getAbsolutePath());
				ma.gui.ivPR.setVisibility(View.VISIBLE);
				ma.gui.ivPR.setImageResource(R.drawable.ic_play);
				if(ma.shr.getBoolean("showInfoBar",false)){
					ma.gui.status.setVisibility(View.VISIBLE);
				}else{
					ma.gui.ivInfo.setVisibility(View.VISIBLE);
				}
				fbp.editorCanSave=false;
				fbp.file=destination;
				fbp.dyn=2;
				StringBuilder sb=new StringBuilder();
				try{
					FileReader fr=new FileReader(destination);
					int ii=fr.read();
					while(ii>=0){
						sb.append((char)ii);
						ii=fr.read();
					}
					if(fbp.hie==null){fbp.hie=new HtmlIndentEditor(ma);fbp.addView(fbp.hie,new LinearLayout.LayoutParams(-1,0,1));}
					fbp.hie.letText(sb.toString().toCharArray());
					fbp.hie.setVisibility(View.VISIBLE);
				}catch(Exception e){
					log.alert(e.toString());
				}
			}else if(itSelected==itText){
				
                ma.gui.ebi.setVisibility(VISIBLE);
                
				ma.gui.ivQuit.setVisibility(View.VISIBLE);
				ma.gui.ivInto.setVisibility(View.GONE);
				ma.gui.ivInfo.setVisibility(View.VISIBLE);
				ma.gui.ivRW.setImageResource(R.drawable.ic_lead_pencil);
				ma.gui.ivRW.setVisibility(View.VISIBLE);
				ma.gui.ivSave.setColorFilter(IconView.unable);
				ma.gui.ivSave.setVisibility(View.VISIBLE);
				ma.gui.ivRefresh.setColorFilter(IconView.unable);
				ma.gui.ivRefresh.setVisibility(View.VISIBLE);
				ma.gui.status.setText(destination.getAbsolutePath());
                
				
				if(fbp.der==10||fbp.der==12){
					fbp.dyn=12;
				}else if(fbp.der==11){
					fbp.dyn=12;
					fbp.hb.webView.loadUrl("about:blank");
				}else{
					fbp.dyn=1;
				}
				try{
					StringBuilder sb=new StringBuilder();
					FileReader fr=new FileReader(destination);
					int ii=fr.read();
					while(ii>=0){
						sb.append((char)ii);
						ii=fr.read();
					}
					if(fbp.ie==null){
						fbp.ie=new IndentEditor(ma);
						fbp.addView(fbp.ie,new LinearLayout.LayoutParams(-1,0,1));
					}
                    ma.gui.ebi.to=fbp.ie;
					String s=sb.toString();
                    fbp.editorCanSave=false;
                    fbp.file=destination;
                    fbp.dyn=1;
                    fbp.dlv.setVisibility(GONE);
                    fbp.fn.setVisibility(GONE);
                    fbp.toolsbar.setVisibility(View.GONE);
					fbp.ie.letText(s.toCharArray());
					fbp.ie.setVisibility(View.VISIBLE);
                    //log.alert("fbp.dlv is gone");
				}catch(Exception e){
					log.alert(e.toString());
				}
			}
			ma.wm.removeView(this);
		}else if(v==itText){
			itSelected=itText;
			itText.invalidate();
			itHtml.invalidate();
		}else if(v==itHtml){
			itSelected=itHtml;
			itText.invalidate();
			itHtml.invalidate();
		}
	}
	@Override
	protected void onDraw(Canvas canvas){
		Paint p=new Paint();
		p.setColor(FileBrowserPage.borderColor);
		p.setStyle(Paint.Style.FILL);
		canvas.drawColor(0xffffffff);
		canvas.drawRect(0,0,FileBrowserPage.borderWidth,getHeight(),p);
		canvas.drawRect(0,0,getWidth(),FileBrowserPage.borderWidth,p);
		canvas.drawRect(getWidth()-FileBrowserPage.borderWidth,0,getWidth(),getHeight(),p);
		canvas.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
		super.onDraw(canvas);
	}
	public void show(FileBrowserPage fbp){
		this.fbp=fbp;
		if(openInNewTab){
			title.setText("打开文档");
			tbOpen.setText("打开");
		}else{
			title.setText("查看文档");
			tbOpen.setText("查看");
		}
		if(destination.length()>MainActivity.FILE_LENGTH_LIM){
			itText.enabled=false;
			itText.setTextColor(0xffff0000);
		}else{
			itText.enabled=true;
		}
		wlp.width=fbp.getWidth()*3/4;
		wlp.height=-2;
		ma.wm.addView(this,wlp);
	}
	class Item extends TextView{
		boolean enabled=true;
		Item(String s){
			super(ma);
			setText(s);
			setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
			setTextColor(0xff000000);
			setPadding(9,0,0,0);
			setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
			StateListDrawable sd=new StateListDrawable();
			sd.addState(new int[]{android.R.attr.state_pressed},new ColorDrawable(0x77888888));
			setBackground(sd);
		}
		@Override
		protected void onDraw(Canvas canvas){
			super.onDraw(canvas);
			if(itSelected==this){
				Paint p=getPaint();
				int paddings=tick.getHeight()/8;
				int offset=(int)getTextSize();
				Rect src=new Rect(paddings,paddings,tick.getWidth(),tick.getHeight()-paddings);
				Rect dst=new Rect(getWidth()-getHeight()-offset,0,getWidth()-offset,getHeight());
				canvas.drawBitmap(tick,src,dst,p);
			}
		}
	}
}
